(function()
{
	var cmdFun = function(options)
	{
		this.id = getRandId();
		this.html = '';
		if (options)
		{
			this.options = $.extend({
				'cmdlist' : {
					'cmd1' : {
						'issearch' : true,
						// li 级别单类 noellipsis 表示不使用省略号
						'liclassname' : '',
						// 为搜索时，这是提示语句
						'placeholder' : '',
						// 是否隐藏
						'hidden' : false,
						// 标题，如果没有标题，则需要减少右边边距，在classname中自动添加类 notext
						'caption' : 'caption1',
						// 图标和式样
						'icon' : 'fa-list',
						// 如果希望分组，可手动添加类 notextb
						'classname' : '',
						// 右键菜单，如果有，则弹出右键菜单，回调函数返回值和标题 {'key' : 'caption'}, 对应的 fun将获得key参数
						'rightmenuarr' : null,
						'fun' : function(id, name)
						{
						},
					}
				},
				'classname' : '',
				// 所属tableid，即table.id
				'tableid' : '',
				// 是否为单行显示，默认为false
				'single' : false,
			}, options);
			app.templete(this);
			this._setEvent();
			this._show();
		}
	};
	/**
	 * 取得当前对象
	 */
	cmdFun.prototype.getThis = function(obj)
	{
		var k = $(obj).parents('.cmdbox');
		if (k.length > 0)
		{
			return app.templete(k[0].id);
		}
		else
			return null;
	}
	/**
	 * 设置事件
	 */
	cmdFun.prototype._setEvent = function()
	{
		// 下面只执行一次
		if (cmdFun.prototype._)
			return;
		cmdFun.prototype._ = 1;
		var that = this;
		// 操作按钮
		$('body').on('click', '.cmdbox .cmd', function()
		{
			var k = $(this).attr('data');
			var ta = that.getThis(this);
			if (!ta)
				return;
			if ($(this).hasClass('rightmenu'))
			{
				var a = ta.options.cmdlist[k].rightmenuarr;
				if (!a || !ta.options.tableid)
					return;
				var table = app.templete(ta.options.tableid);
				if (!table)
					return;
				var tleft = table.getCmdOffset(k);
				var ttop = table.getCmdOffset('_refresh') || tleft;
				if (ttop)
				{
					var thisobj = this;
					app.rightMenu(tleft.left, ttop.top, 0, a, function(st, stname)
					{
						run(k, st, stname);
						if ($(thisobj).find(' .fa-filter').length > 0)
							table.cmdCaption(k, st ? stname : '');
					});
				}
				return false;
			}
			else if (k == '_more_')
			{
				var alist = {};
				var i = 0;
				$('#' + ta.id + ' li').each(function()
				{
					var key = $(this).attr('key');
					if ($(this).hasClass('hidden'))
						return;
					if (key)
					{
						i++;
						alist[i] = this.outerHTML.replace(/display\s{0,6}:\s{0,6}none/, '');
						alist[i] = alist[i].replace('style=";"', '');
						alist[i] = alist[i].replace('display', '');
					}
				});
				// 快捷菜单(折叠起来的)
				var left = $(this).offset().left;
				var top = $(this).offset().top;
				var cla = 'cmd' + ta.id;
				if (ta.options.tableid)
					cla += ' table' + ta.options.tableid;
				app.rightMenu(left, top, 0, alist, function(k, k2, thisobj)
				{
					return run(k, undefined, undefined, thisobj);
				}, cla);
			}
			else
				run(k);
			function run(k, st, stname, thisobj)
			{
				if (k && ta.options && ta.options.cmdlist && ta.options.cmdlist[k])
				{
					var a = ta.options.cmdlist[k].rightmenuarr;
					if (a && ta.options.tableid && st === undefined)
					{
						var table = app.templete(ta.options.tableid);
						if (!table)
							return;
						var tleft = table.getCmdOffset(k);
						var ttop = table.getCmdOffset('_refresh');
						app.rightMenu(tleft.left, ttop.top, 0, a, function(st, stname)
						{
							run(k, st, stname);
							if (thisobj && $(thisobj).find(' .fa-filter').length > 0)
								table.cmdCaption(k, st ? stname : '');
						});
						return false;
					}
					else if (ta.options.cmdlist[k]['fun'])
					{
						var fun = ta.options.cmdlist[k]['fun'];
						if (fun)
						{
							if (ta.options.cmdlist[k]['confirm'])
							{
								confirm(ta.options.cmdlist[k]['confirm'], function()
								{
									if (arguments.length <= 1)
										fun();
									else
									{
										if (ta.options.cmdlist[k]['rightmenuarr'])
										{
											var table = (new tableFun).getThis($('#' + ta.id)[0]);
											if (table)
												table.cmdCaption(k, st ? stname : '');
										}
										ta.cmdCaption(k, stname);
										fun(st, stname);
									}
								});
							}
							else
							{
								if (arguments.length <= 1)
									fun();
								else
								{
									if (ta.options.cmdlist[k]['rightmenuarr'])
									{
										var table = (new tableFun).getThis($('#' + ta.id)[0]);
										if (table)
											table.cmdCaption(k, st ? stname : '');
									}
									fun(st, stname);
								}
							}
						}
					}
				}
			}
		});
		// 搜索框回车
		$('body').on('keydown', '.cmdbox .search input', function(event)
		{
			if (event.keyCode == 13)
			{
				var ta = that.getThis(this);
				ta.submitInput(this, true);
			}
		});
		// 搜索框离开焦点
		$('body').on('blur', '.cmdbox .search input', function(event)
		{
			var ta = that.getThis(this);
			ta.submitInput(this);
		});
	};
	/**
	 * 聚合框，生成一个快捷菜单，把已经隐藏的对象聚合起来
	 */
	cmdFun.prototype.showMoreLeft = function(delay)
	{
		var that = this;
		if (!delay)
		{
			if (this.showMoreLeft.timer)
				clearTimeout(this.showMoreLeft.timer);
			this.showMoreLeft.timer = setTimeout(function()
			{
				that.showMoreLeft(true);
			}, 500);
			return;
		}
		var offset = 12;
		var span = 42; // 留给多余空间
		var cm = $('#' + this.id).parent();
		if (cm.length == 0)
			return;
		var pleft = cm.offset().left + offset;
		$('#' + this.id + ' ._more_').remove();
		var lasthide = null;
		var firstshow = null;
		var lastleft = 0;
		var hiddenn = 0;
		var that = this;
		var blueclass = '';
		$('#' + this.id + ' li').removeClass('hideformore');
		$('#' + this.id + ' li').css('display', '');
		$('#' + this.id + ' li').each(function()
		{
			if ($(this).hasClass('cmd'))
			{
				var left = $(this).offset().left;
				if (left < pleft + span && $(this).hasClass('fa'))
				{
					hiddenn++;
					$(this).hide();
					$(this).addClass('hideformore');
					$(this).attr('key', $(this).attr('data'));
					lasthide = this;
					lastleft = left;
					if ($(this).hasClass('blue'))
						blueclass = 'blue';
				}
				else
				{
					$(this).show();
					$(this).removeClass('hideformore');
					$(this).attr('key', '');
					if (!firstshow)
						firstshow = this;
				}
			}
		});
		if (hiddenn == 1 && lastleft >= pleft)
			$(lasthide).show();
		else if (hiddenn > 0)
		{
			var s = getIC('th-large cmd _more_ ' + blueclass, 'li', 'data="_more_"');
			if (firstshow)
				$(firstshow).before(s);
			else
				$('#' + this.id).append(s);
		}
	}
	// 取得或设置标题
	cmdFun.prototype.caption = function(na, caption)
	{
		if (arguments.length < 2)
			return $('#' + this.id + ' .' + na).html();
		$('#' + this.id + ' .' + na).html(caption);
		return caption;
	}
	// 提交输入框函数
	cmdFun.prototype.submitInput = function(obj, must, sure)
	{
		var that = this;
		var k = $(obj).parent().attr('data');
		var ta = that.getThis(obj);
		if (ta && ta.options && ta.options.cmdlist && ta.options.cmdlist[k] && ta.options.cmdlist[k]['fun'])
		{
			var fun = ta.options.cmdlist[k]['fun'];
			if (fun)
			{
				if (ta.options.cmdlist[k]['oldvalue'] === undefined)
					ta.options.cmdlist[k]['oldvalue'] = '';
				var v = $(obj).val();
				if (!must && v == ta.options.cmdlist[k]['oldvalue'])
					return;
				if (!sure && ta.options.cmdlist[k]['confirm'])
				{
					confirm(ta.options.cmdlist[k]['confirm'], function()
					{
						that.submitInput(obj, must, true);
					});
					return;
				}
				ta.options.cmdlist[k]['oldvalue'] = v;
				if (fun(v) === true)
					$(obj).val('');
			}
		}
	}
	// 显示cmd
	cmdFun.prototype._show = function()
	{
		var t = '<ol class="cmdbox breadcrumbinline cmdfun ' + this.options.classname + '" id="' + this.id + '"';
		if (this.options.single)
			t += ' style="white-space:nowrap" ';
		t += '>';
		for ( var k in this.options.cmdlist)
		{
			if (!this.options.cmdlist[k] || this.options.cmdlist[k].hidden)
				continue;
			var cap = this.options.cmdlist[k].caption;
			var issearch = this.options.cmdlist[k].issearch;
			var placeholder = this.options.cmdlist[k].placeholder;
			if (!placeholder)
				placeholder = '';
			var fun = this.options.cmdlist[k].fun;
			var cla = this.options.cmdlist[k].icon || '';
			var kcla = k;
			if (this.options.cmdlist[k].classname)
				kcla += ' ' + this.options.cmdlist[k].classname;
			if (cap)
				cap = '<span class="cap">' + cap + '</span>';
			else
				kcla += ' notext';
			if (issearch)
			{
				var re = ' editabled="true" ';
				var inclas = '';
				if (app.needWTPJp())
				{
					re = ' readonly="true" editabled="true" ';
					inclas = 'editabled';
				}
				var inp = '<input ' + re;
				if (placeholder)
					inp += ' placeholder="' + placeholder + '"';
				if (inclas)
					inp += ' class="' + inclas + '"';
				if (issearch !== true)
				{
					issearch = (issearch + ' ').trim();
					if (issearch.indexOf("\"") < 0)
					{
						inp += ' value="' + issearch + '"';
					}
				}
				inp += '>';
				if (this.options.cmdlist[k].liclassname)
					kcla += ' ' + this.options.cmdlist[k].liclassname;
				t += '<li data="' + k + '" class="' + kcla + ' search fa">' + getIC(cla, 'span') + '</span>' + cap + inp + '</li>';
			}
			else
			{
				var sa = '';
				if (this.options.cmdlist[k].rightmenuarr)
					sa = ' rightmenu';
				if (this.options.cmdlist[k].liclassname)
					sa += ' ' + this.options.cmdlist[k].liclassname;
				t += '<li data="' + k + '" class="' + kcla + ' cmd fa' + sa + '">' + getIC(cla, 'span') + cap + '</li>';
			}
		}
		t += '</ol>';
		this.html = t;
	}
	window.cmdFun = cmdFun;
}());
